var 
	gulp        = require("gulp"),
	concat      = require("gulp-concat"),
	connect     = require("gulp-connect"),
	uglify      = require("gulp-uglify"),
	less        = require("gulp-less"),
	minifyCSS   = require('gulp-minify-css'),
    autoprefix  = require('gulp-autoprefixer'),
	webpack     = require('webpack-stream'),
	named       = require('vinyl-named'),
	fileConfigs = require('./config.json');

// gulp.task("iconfont", function(){
// gulp.src([
// 		"src/css/iconfont/iconfont.eot",
// 		"src/css/iconfont/iconfont.svg",
// 		"src/css/iconfont/iconfont.ttf",
// 		"src/css/iconfont/iconfont.woff"])
// .pipe(gulp.dest("dist/css"))
// .pipe(connect.reload());
// });

gulp.task("html", function(){
	gulp.src("src/pages/**/*.html")
	.pipe(gulp.dest("dist/pages"));
});

gulp.task("mock", function(){
	gulp.src("src/mock/*.json")
	.pipe(gulp.dest("dist/mock"));
});

gulp.task("images", function(){
	gulp.src("src/static/images/*")
	.pipe(gulp.dest("dist/static/images"));
});

gulp.task("bundleLess", function(){
	gulp.src(fileConfigs.css)
	.pipe(less())
	.pipe(autoprefix())
    //.pipe(minifyCSS())
	.pipe(gulp.dest("dist/static/css"))
	.pipe(connect.reload());
});

gulp.task("bundleCommonCss",function() {
	gulp.src("src/static/css/base/*.css")
	.pipe(concat("bundle.css"))
	//.pipe(minifyCSS())
	.pipe(gulp.dest("dist/static/base/"))
	.pipe(connect.reload());
});

gulp.task("bundleCommonJs",function() {
	gulp.src("src/static/js/base/*.js")
	.pipe(concat("bundle.js"))
	//.pipe(uglify())
	.pipe(gulp.dest("dist/static/base/"))
	.pipe(connect.reload());
});

gulp.task('bundleJs', function() {
    return gulp.src(fileConfigs.js)
        .pipe(named())
        .pipe(webpack({
            output: {
                filename: '[name].js'
            },
            module: {
                loaders: [{
                    test: /\.mustache$/,
                    loader: 'mustache'
                }, {
                    test: /\.js$/,
                    loader: "imports-loader?define=>false"
                }]
            },
            devtool: "#eval-source-map"
        }))
        // .pipe(uglify().on('error', function(e) {
        //     console.log('\x07',e.lineNumber, e.message);
        //     return this.end()}
        // ))
        .pipe(gulp.dest("dist/static/js/"));
});

gulp.task("layui", function() {
	gulp.src('src/static/plugin/layui/*.js')
	 //.pipe(uglify())
    .pipe(gulp.dest("dist/static/plugin/layui/"));

	gulp.src('src/static/plugin/layui/css/**/*.css')
	//.pipe(minifyCSS())
	.pipe(gulp.dest('dist/static/plugin/layui/css/'));

	 gulp.src('src/static/plugin/layui/font/*')
    .pipe(gulp.dest('dist/static/font/'));

    gulp.src('src/static/plugin/layui/images/face/*.gif')
    .pipe(gulp.dest('dist/static/plugin/layui/images/face/'));

    gulp.src('src/static/plugin/layui/lay/modules/*.js')
    //.pipe(uglify())
    .pipe(gulp.dest("dist/static/plugin/layui/lay/modules/"));

    gulp.src('src/static/plugin/layui/css/modules/layer/default/*.{png,gif}')
    .pipe(gulp.dest('dist/static/plugin/layui/css/modules/layer/default/'));
});

gulp.task("build", function() {
	gulp.src('src/static/plugin/build/js/*.js')
	//.pipe(uglify())
	.pipe(gulp.dest("dist/static/plugin/build/js/"));

	gulp.src('src/static/plugin/build/css/**/*.css')
	//.pipe(minifyCSS())
	.pipe(gulp.dest('dist/static/plugin/build/css/'));
});


gulp.task("zTree", function() {
	gulp.src('src/static/plugin/zTree/css/img/*.{png,gif}')
    .pipe(gulp.dest('dist/static/plugin/zTree/css/img/'));

	gulp.src('src/static/plugin/zTree/css/*.css')
	.pipe(gulp.dest('dist/static/plugin/zTree/css/'));

	gulp.src('src/static/plugin/zTree/js/*.js')
	.pipe(gulp.dest('dist/static/plugin/zTree/js/'));
});
gulp.task("ztreeImgs",function(){
	gulp.src('src/static/plugin/build/css/ztree/metro/img/*')
	.pipe(gulp.dest('dist/static/plugin/build/css/ztree/metro/img/'))
});

gulp.task("watch",function(){
	gulp.watch("src/static/js/**/*", ["bundleJs"]);
	gulp.watch("src/static/js/base/*.js",["bundleCommonJs"]);
	gulp.watch("src/static/css/**/*.less",["bundleLess"]);
	gulp.watch("src/static/css/base/*.css",["bundleCommonCss"]);
	gulp.watch("src/pages/**/*.html",["html"]);
	gulp.watch("src/mock/*",["mock"]);
});

gulp.task("server",function(){
	connect.server({
		root:"dist",
		port:3000,
		livereload:true
	})
})

gulp.task("default", ["html","mock","images","bundleLess","bundleCommonCss","bundleCommonJs","bundleJs","layui","zTree","ztreeImgs","build","watch", "server"]);